/**
 * 统考科目管理
 */

<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="courseName">
          <span>科目名称：</span>
          <el-input
            v-model="subjectName"
            placeholder="请输入科目名称"
            style=""
          />
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="searchInput">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>
    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAdd">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="batchDel">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
        </div>
      </div>
      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50"> </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50"
          ></el-table-column>
          <el-table-column
            prop="subjectName"
            label="科目名称"
          ></el-table-column>
          <el-table-column prop="createTime" label="创建时间"></el-table-column>
          <el-table-column prop="createBy" label="创建人"></el-table-column>

          <el-table-column prop="action" label="操作">
            <template slot-scope="scope">
              <span
                class="icon edit"
                @click="handleEdit(scope.row)"
              >编辑</span>
              <span
                class="icon del"
                @click="handleDel(scope.row.subjectId)"
              >删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="tableData.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 新增班级 -->
    <div class="dialog">
      <el-dialog
        :title="isEdit ? '编辑科目' : '添加科目'"
        :visible.sync="dialogFormVisible"
        center
      >
        <el-form :model="form">
          <el-form-item label="科目名称:" :label-width="formLabelWidth">
            <el-input
              v-model="form.subjectName"
              autocomplete="off"
              placeholder="请输入科目名称"
              style="width: 270px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="closeAdd">取 消</el-button>
          <el-button type="primary" @click="confirmAdd">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { _debounce } from "@/utils/debounce";
import {
  addExamsubjectsApi,
  delExamsubjectsApi,
  editExamsubjectsApi,
  getExamsubjectsManagementListApi,
} from "@/api/examManagement/examsubjectsManagement";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      date: false,
      subjectName: "",
      multipleSelection: [],
      currentPage: 1,
      pageSize: 10,
      dialogFormVisible: false,
      form: {
        subjectId: "",
        subjectName: "",
      },
      formLabelWidth: "100px",
      isEdit: false,
    };
  },

  mounted() {
    // 获取科目列表
    this.getExamsubjectsManagementList({
      pageNum: this.currentPage,
      pageSize: this.pageSize,
    });
  },

  methods: {
    // 获取班级列表
    getExamsubjectsManagementList(data) {
      getExamsubjectsManagementListApi(data).then((result) => {
        if (result.code === 200) {
          let data = result.rows.map((item) => {
            return {
              ...item,
              status: item.status == "0" ? true : false,
            };
          });

          this.tableData = data;
          this.total = result.total;
        }
      });
    },

    // 搜索
    searchInput: _debounce(function () {
      // 获取科目列表
      this.getExamsubjectsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        subjectName: this.subjectName,
      });
    }, 200),

    // 重置
    refreshInput: _debounce(function () {
      this.subjectName = "";
      // 获取科目列表
      this.getExamsubjectsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      });
    }, 200),

    // 新增课程
    openAdd() {
      this.form = {
        subjectId: "",
        subjectName: "",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    // 编辑课程
    handleEdit(data) {
      let { subjectName, subjectId } = data;
      this.form = {
        subjectName,
        subjectId: subjectId,
      };
      this.isEdit = true;
      this.dialogFormVisible = true;
    },

    // 取消
    closeAdd() {
      this.dialogFormVisible = false;
      this.form = {
        subjectName: "",
        subjectId: "",
      };
    },

    //确定新增 / 编辑
    confirmAdd: _debounce(function () {
      let { subjectName } = this.form;
      if (subjectName) {
        let arr = this.tableData.filter((item) => {
          return item.subjectName == subjectName;
        });
        if (arr.length === 0) {
          if (this.isEdit) {
            editExamsubjectsApi(this.form).then((res) => {
              if (res.code === 200) {
                this.$message.success("修改成功！");
                this.getExamsubjectsManagementList({
                  pageNum: this.currentPage,
                  pageSize: this.pageSize,
                });
              } else {
                this.$message.error("修改失败！");
              }
            });
          } else {
            addExamsubjectsApi({ ...this.form }).then((res) => {
              if (res.code === 200) {
                this.$message.success("添加成功！");
                this.getExamsubjectsManagementList({
                  pageNum: this.currentPage,
                  pageSize: this.pageSize,
                });
              } else {
                this.$message.error("添加失败！");
              }
            });
          }
          this.dialogFormVisible = false;
        } else {
          this.$message.error("科目名称已存在！");
        }
      } else {
        this.$message.error("科目名称不能为空！");
      }
    }, 200),

    // 删除
    handleDel(ids) {
      this.$confirm("此操作将删除该科目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delExamsubjectsApi({ ids }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功！");
              this.getExamsubjectsManagementList({
                pageNum: this.currentPage,
                pageSize: this.pageSize,
              });
            } else {
              this.$message.error("删除失败！");
            }
          });
        })
        .catch(() => {});
    },

    // 批量删除
    batchDel() {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.subjectId;
        });
        this.$confirm("此操作将删除已选择的科目, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delExamsubjectsApi({ ids: data.join(",") }).then((res) => {
              if (res.code === 200) {
                this.$message.success("删除成功！");
                this.getExamsubjectsManagementList({
                  pageNum: this.currentPage,
                  pageSize: this.pageSize,
                });
              } else {
                this.$message.error("删除失败！");
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.error("请选择要删除的科目！");
      }
    },

    // 已选中项
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      // 获取科目列表
      this.getExamsubjectsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        subjectName: this.subjectName,
      });
    },

    // 跳转指定页面
    goPage() {
      let { value } = this.$refs.currentPage;
      this.currentPage = Number(value);
      // 获取科目列表
      this.getExamsubjectsManagementList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        subjectName: this.subjectName,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}

.edit{
    color: #1990FF;
    cursor: pointer;
  }
  .del{
    color: #1990FF;
    margin-left: 10px;
    cursor: pointer;
  }
.block {
  height: 40px;
  border: solid 1px #dcdfe6;
  border-radius: 4px;
  display: flex;
  align-items: center;
  box-sizing: border-box;

  .el-date-editor.el-input,
  .el-date-editor.el-input__inner {
    max-width: 200px;
  }

  ::v-deep .el-input__inner {
    height: 38px;
    border: none;
  }

  ::v-deep .el-icon-arrow-down {
    color: #c2c2c2;
    padding: 0 10px 0 0;
  }

  ::v-deep .el-icon-arrow-up {
    color: #c2c2c2;
    padding: 0 10px 0 0;
  }
}

// 弹框样式
.dialog {
  ::v-deep .el-dialog {
    max-width: 750px;
    min-width: 500px;
    min-height: 426px;
    border-radius: 10px;
    background-color: #f9f9f9;
  }

  ::v-deep .el-dialog__header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background-color: #4895db;
    padding: 0;
    border-radius: 10px 10px 0 0;

    span {
      font-size: 16px;
      color: #fff;
    }
  }

  ::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  ::v-deep .el-dialog__headerbtn {
    top: 15px;
  }

  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 18px;
  }

  ::v-deep .el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 80px 0 100px 0;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  ::v-deep .el-form-item__content {
    width: 280px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }

  ::v-deep .el-dialog__footer {
    padding: 10px 20px 50px;
  }

  ::v-deep .el-button--default {
    width: 130px;
    border-radius: 20px;
    background-color: #f0f0f0;
    color: #aaaaaa;
    border: none;
  }

  ::v-deep .el-button + .el-button {
    width: 130px;
    border-radius: 20px;
    margin-left: 50px;
    background-color: #4895db;
    border-color: #4895db;
  }
}
</style>